﻿// 开发者:micropu
// 公司:佛山远钧智慧科技有限公司

using Furion.DatabaseAccessor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.NET.Application;

/// <summary>
/// 大屏数据
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class BigDataService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Project> _projectRep;
    private readonly SqlSugarRepository<UploadLog> _uploadLogRep;

    public BigDataService(SqlSugarRepository<Project> projectRep, SqlSugarRepository<UploadLog> uploadLogRep)
    {
        _projectRep = projectRep;
        _uploadLogRep = uploadLogRep;
    }

    /// <summary>
    /// 取项目信息
    /// </summary>
    /// <param name="ProjectID"></param>
    /// <returns></returns>
    [HttpGet("/api/getProjectInfo")]
    [AllowAnonymous]
    [UnitOfWork]
    public async Task<object> getProjectInfo([FromQuery] long ProjectID)
    {
        return await _projectRep.GetFirstAsync(u => u.Id == ProjectID);
    }


    /// <summary>
    /// 前10条打卡记录
    /// </summary>
    /// <param name="ProjectID"></param>
    /// <returns></returns>
    [HttpGet("/api/TopPass")]
    [AllowAnonymous]
    [UnitOfWork]
    public async Task<List<UploadLog>> TopPass([FromQuery] long ProjectID)
    {
       return await _uploadLogRep   
            .AsQueryable()
            .ClearFilter() // 清除过滤条件  
            .OrderBy(u => u.CreateTime, OrderByType.Desc)
            .Take(6)
            .ToListAsync();
    }

    /// <summary>
    /// 项目当天打卡人数统计，总人数，今天打卡，进出，出场，在场，不在场
    /// </summary>
    /// <param name="ProjectID"></param>
    /// <returns></returns>
    [HttpGet("/api/EmpSum")]
    [AllowAnonymous]
    [UnitOfWork]
    public async Task<object> EmpSum([FromQuery] long ProjectID)
    {

        var query =await  _projectRep.AsQueryable()
                    .Where(it => it.Id == ProjectID)
                    .Select(it => new
                    {
                        EmpCount = SqlFunc.Subqueryable<Employee>().Count(),
                        TodayUploadCount = SqlFunc.Subqueryable<UploadLog>().Where(u => u.CreateTime.Date == DateTime.Now.Date).Count(),
                        TodayUploadCount_in = 0,
                        TodayUploadCount_out = 0

                    }).FirstAsync();
       return query;
    }
}
